class ClickBut extends eui.Component{
	private Start:eui.Group;//开始层

	private EndView:eui.Group;//结束

	public startBut:eui.Button;//开始按钮

	private HelpNum:eui.Button;//获取次数按钮

	public cishuNume:eui.Image; // 次数值

	public cishuNume2:eui.Image; // 次数值

	public cishu=5;	//次数值

	public close:eui.Button;//关闭按钮

	public anewBut:eui.Button;//重新开始

	public helpBut:eui.Button;//获取次数

	public paihang:eui.Button;//排行榜

	public fenxiang:eui.Button;//分享

	public one:eui.Image;//1
	
	public two:eui.Image;//2
	
	public three:eui.Image;//3
	
	public four:eui.Image;//4

	private bitmap: egret.Bitmap;
    
	private isdisplay = false;
    
	private rankingListMask: egret.Shape;

	public datNum=null;

	public who = null;

	public constructor() {
		super();
		
		this.skinName="resource/eui_skins/Start.exml";
		
	};


	// 开始按钮
	public start(){
		
		this.Start.visible=true;
		
		this.startBut.addEventListener(egret.TouchEvent.TOUCH_TAP,this.startFun,this);
		
		if( Number(localStorage.getItem("ciNum")) == 0){
			console.log("不能点击");
			this.HelpNum.visible = true;
		}else{
			this.HelpNum.visible = false;
		}

		this.HelpNum.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onFenxiang, this);
		
		// this.HelpNum.addEventListener(egret.TouchEvent.TOUCH_TAP, this.helpFun, this);
	};

	// 开始点击事件 进入游戏
	private startFun() {
		if( Number(localStorage.getItem("ciNum")) == 0){
			console.log("不能点击");
		}else{
			this.who = 0;

			var changeEvent = new ChangeEvent(ChangeEvent.CHANGE_EVENT);
			
			changeEvent.eventType = GameagentView.GAMEAGENT_VIEW;
			
			changeEvent.obj = this;
			
			SwitcherView.getInstance().dispatchEvent(changeEvent);
		}
		
		
	};

	// 结束时显示 
	public endGame(a){
		this.who = 2;
		this.EndView.visible=true;
		
		if(a > 0){
			// 再来一次
			this.anewBut.visible=true;
			
			this.anewBut.addEventListener(egret.TouchEvent.TOUCH_TAP,this.anewFun,this);
		}else{
			// 获取次数
			this.helpBut.visible=true;
			
			this.helpBut.addEventListener(egret.TouchEvent.TOUCH_TAP,this.helpFun,this);
		};
		
		// 排行榜
		this.paihang.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onButtonClick, this);
	
		// 分享
		this.fenxiang.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onFenxiang, this);
	};

	// 再来一局函数 进入克隆
	private anewFun() {
		var changeEvent = new ChangeEvent(ChangeEvent.CHANGE_EVENT);
		changeEvent.eventType = CloneView.CLONEVIEW;
		changeEvent.obj = this;
		SwitcherView.getInstance().dispatchEvent(changeEvent);
	};
	
	// 获取次数
	private helpFun() {
		var url = "http://minijiangshi.icaoyao.vip:8082/miniGames/addTimes";
		var type = egret.HttpMethod.POST;
		var data = "gameName=daoshi&wid="+localStorage.getItem('openid')+"&type=1";
		AjaxData.Ajax( url, type, data);
		AjaxData.axios.addEventListener(egret.Event.COMPLETE,this.onPostCompletes,this);
        AjaxData.axios.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onPostIOErrors,this);
		var changeEvent = new ChangeEvent(ChangeEvent.CHANGE_EVENT);
		changeEvent.eventType = GuangGame.GUANGGAO;
		changeEvent.obj = this;
		SwitcherView.getInstance().dispatchEvent(changeEvent);
	}

	// 克隆
	public endGames(a){
		this.who = 1;
		this.EndView.visible=true;
		if(a> 0){
			this.anewBut.visible=true;
			this.anewBut.addEventListener(egret.TouchEvent.TOUCH_TAP,this.anewFuns,this);
		}else{
			this.helpBut.visible=true;
			this.helpBut.addEventListener(egret.TouchEvent.TOUCH_TAP,this.helpFun,this);
		}
		// 排行榜
		this.paihang.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onButtonClick, this);

		// 分享
		this.fenxiang.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onFenxiang, this);
	};

	// 再来一局函数 进入游戏
	private anewFuns() {

		var changeEvent = new ChangeEvent(ChangeEvent.CHANGE_EVENT);
		changeEvent.eventType = GameagentView.GAMEAGENT_VIEW;
		changeEvent.obj = this;
		SwitcherView.getInstance().dispatchEvent(changeEvent);
	};	

	// 排行榜
    private onButtonClick(e: egret.TouchEvent) {
        // console.log("排行榜")
        let platform: any = window.platform;
        if (this.isdisplay) {
            this.bitmap.parent && this.bitmap.parent.removeChild(this.bitmap);
            this.rankingListMask.parent && this.rankingListMask.parent.removeChild(this.rankingListMask);
            this.isdisplay = false;
			this.close.visible = false;
            platform.openDataContext.postMessage({
                isDisplay: this.isdisplay,
                text: 'hello',
                year: (new Date()).getFullYear(),
                command: "close"
            });
        } else {
            //处理遮罩，避免开放数据域事件影响主域。
            this.rankingListMask = new egret.Shape();
            this.rankingListMask.graphics.beginFill(0x000000, 1);
            this.rankingListMask.graphics.drawRect(0, 0, this.stage.width, this.stage.height);
            this.rankingListMask.graphics.endFill();
            this.rankingListMask.alpha = 0.5;
            this.rankingListMask.touchEnabled = true;
            this.addChild(this.rankingListMask);

            //简单实现，打开这关闭使用一个按钮。
            //主要示例代码开始
            this.bitmap = platform.openDataContext.createDisplayObject(null, this.stage.stageWidth, this.stage.stageHeight);
            this.addChild(this.bitmap);
            console.log("关闭按钮出现")
			this.addChild(this.close);//添加排行榜
			this.close.visible = true;
			this.close.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onButtonClick, this)
            //主域向子域发送自定义消息
            platform.openDataContext.postMessage({
                isDisplay: this.isdisplay,
                text: 'hello',
                year: (new Date()).getFullYear(),
                command: "open"
            });
            //主要示例代码结束            
            this.isdisplay = true;
        }


		
    };

	// 移除场景判断   
	public endView(){
		if(this.who == 0){
			this.Start.visible=false;
			if(this.startBut.hasEventListener(egret.TouchEvent.TOUCH_TAP))
				this.startBut.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.startFun,this);
		}else if(this.who == 1){
			this.EndView.visible=false;
			if(this.anewBut.hasEventListener(egret.TouchEvent.TOUCH_TAP))
				this.anewBut.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.anewFun,this);
				this.helpBut.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.helpFun,this);
				
		}else if(this.who == 2){
			this.EndView.visible=false;
			if(this.anewBut.hasEventListener(egret.TouchEvent.TOUCH_TAP))
				this.anewBut.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.anewFuns,this);
				this.helpBut.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.helpFun,this);
		}
	};

	// 分享
	private onFenxiang(){
		const result = platform.share();
		// 增加两次
		var url = "http://minijiangshi.icaoyao.vip:8082/miniGames/addTimes";
		var type = egret.HttpMethod.POST;
		var data = "gameName=daoshi&wid="+localStorage.getItem('openid')+"&type=2";
		AjaxData.Ajax( url, type, data);
		AjaxData.axios.addEventListener(egret.Event.COMPLETE,this.onPostComplete,this);
        AjaxData.axios.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onPostIOError,this);
		
	};
	
	private onPostComplete(e:egret.Event):void{
		var request = <egret.HttpRequest>e.currentTarget;
		console.log(request.response);
		localStorage.setItem("ciNum",request.response);
		let a = localStorage.getItem("ciNum").length;//次数长度

		let b = localStorage.getItem("ciNum");//次数值
		
		let c = "_png";
		// console.log(b)
		this.HelpNum.visible = false;
		if(a == 1){
			this.cishuNume.texture =RES.getRes(b + c);
		}else if(a == 2){
			this.cishuNume2.texture =RES.getRes(b[0] + c);
			this.cishuNume.texture =RES.getRes(b[1] + c);
		}
	} 

	private onPostIOError(e:egret.IOErrorEvent):void{

	}

	private onPostCompletes(e:egret.Event):void{
		var request = <egret.HttpRequest>e.currentTarget;
		// console.log("广告"+request.response);
		localStorage.setItem("ciNum",request.response);
		let a = localStorage.getItem("ciNum").length;//次数长度

		let b = localStorage.getItem("ciNum");//次数值
		
		let c = "_png";
		// console.log(b)
		if(a == 1){
			this.cishuNume.texture =RES.getRes(b + c);
		}else if(a == 2){
			this.cishuNume2.texture =RES.getRes(b[0] + c);
			this.cishuNume.texture =RES.getRes(b[1] + c);
		}
	} 

	private onPostIOErrors(e:egret.IOErrorEvent):void{

	}

}